package com.tb.foryou;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {
    private MainView mainView;

    //时间未到
    private static final int DAY_BEFORE = -1;

    //时间格式
    private SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    //按钮
    private Button buttonHead;
    private Button buttonNow;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化登录界面
        initLockView();
        //初始化mainView
        mainView = (MainView) findViewById(R.id.main_view);
        mainView.init(getDayToStart());
        //初始化按钮
        buttonHead = (Button) findViewById(R.id.button_head);
        buttonHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mainView.goHeadPage();
            }
        });
        buttonNow = (Button) findViewById(R.id.button_now);
        buttonNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mainView.goNowPage();
            }
        });
    }

    private void initLockView() {
        //本地加载配置信息，检测是否已经登陆过
        SharedPreferences sharedPreferences= getSharedPreferences("USER_IS_LOGIN",Activity.MODE_PRIVATE);
        boolean isLogin=sharedPreferences.getBoolean("USER_IS_LOGIN",false);
        //如果未登录就显示锁屏
        if(!isLogin){
            //跳至锁屏页
            Intent intent = new Intent(this,LockActivity.class);
            startActivity(intent);
        }
    }

    private int getDayToStart(){
        //起始日期
        Date startDate = null;
        try {
            startDate = df.parse(getResources().getString(R.string.start_date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //现在日期
        Date nowDate = new Date();
        //记录现在的天数
        int dayToStart;
        //获取天数

        dayToStart = (int) ((nowDate.getTime()-startDate.getTime())/(24*60*60*1000));
        //调整天数
        if((nowDate.getTime()-startDate.getTime()) < 0){
            dayToStart = DAY_BEFORE;
        }

        Log.d("startDate","" + startDate);
        Log.d("nowDate","" + nowDate);
        Log.d("dayToStart","" + dayToStart);
        return dayToStart;
    }


    //双击退出实现
    private boolean isExiting;
    private void doubleBackForExit(){
        if (isExiting){
            finish();
        }else{
            Toast.makeText(this,"再戳一下人家就要挂掉了😥",Toast.LENGTH_SHORT).show();
            isExiting = true;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    isExiting = false;
                }
            },2000);
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_BACK:
                doubleBackForExit();
                break;
            default:
                super.onKeyUp(keyCode,event);
        }
        return true;
    }
}
